﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using Shell32;

namespace Folder_Control
{
    public interface Link
    {
        string LinkPath { get; set; }
    }

    public class LinkFolder : Folder, Link
    {
        public string LinkPath { get; set; }

        public LinkFolder(string name, string path, ItemType itemType, Icon icon, string linkPath)
            : base(name, path, icon)
        {
            Name = name;
            Path = path;
            Type = itemType;
            Icon = icon;
            LinkPath = linkPath;
        }
    }

    public class LinkFile : File, Link
    {
        public string LinkPath { get; set; }

        public LinkFile(string name, string path, ItemType itemType, Icon icon, string linkPath)
            : base(name, path, icon)
        {
            Name = name;
            Path = path;
            Type = itemType;
            Icon = icon;
            LinkPath = linkPath;
        }
    }

    // Static class to deal with shortcut files (.lnk) returning either a file or folder as appropriate
    public static class LinkFactory
    {
        public static Item GetLink(string name, string path, Icon icon)
        {
            Shell shell = new Shell();
            FolderItem folderItem = shell.NameSpace(System.IO.Path.GetDirectoryName(path)).ParseName(System.IO.Path.GetFileName(path));
            
            Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;

            string targetName = link.Target.Name;
            string targetPath = link.Target.Path;

            // If target of link is a file return a File object
            if (System.IO.File.Exists(targetPath))
            {
                return new LinkFile(name.Substring(0, name.LastIndexOf(".")), targetPath, ItemType.Link, icon, path);
            }
            else if (Directory.Exists(targetPath)) // else if the target of the link is a directory return a Folder object
            {
                return new LinkFolder(name.Substring(0, name.LastIndexOf(".")), targetPath, ItemType.Link, icon, path);
            }
            else 
            {
                // Check if your looking in the wrong Program Files folder
                if (targetPath.Contains(" (x86)"))
                {
                    targetPath = targetPath.Replace(" (x86)", String.Empty);        

                    if (System.IO.File.Exists(targetPath))
                    {
                        return new LinkFile(name.Substring(0, name.LastIndexOf(".")), targetPath, ItemType.Link, icon, path);
                    }
                    else if (Directory.Exists(targetPath)) // else if the target of the link is a directory return a Folder object
                    {
                        return new LinkFolder(name.Substring(0, name.LastIndexOf(".")), targetPath, ItemType.Link, icon, path);
                    }
                }

                return new ErrorItem(name.Substring(0, name.LastIndexOf(".")), path, icon, "Could not find path to target folder, when item was created.");

                //// file was not a File or Directory so throw an error 
                //throw new FileNotFoundException("When creating a link file was not found.", targetPath);
            }
        }
    }
}
